﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.TextScraping
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PdfSectionFilterComponent:WorkflowComponentBase
    {
        #region props
        private PdfSectionFilterInstruction _Instruction;
        /// <summary>an object that encapsulate _Instruction settings.</summary>
        [Browsable(true)]
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction = (PdfSectionFilterInstruction)value; }
        }

        private PdfSectionFilterOutput _Output;
        /// <summary>an object to encapsulate _Output</summary>
        [Browsable(true)]
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (PdfSectionFilterOutput)value; }
        }

        /// <summary></summary>
        public List<string> HeaderPatterns
        {
            get
            {
                return this._Instruction.HeaderPatterns;
            }
            set
            {
                this._Instruction.HeaderPatterns = value;
            }
        }

        /// <summary></summary>
        public List<string> FooterPatterns
        {
            get
            {
                return this._Instruction.FooterPatterns;
            }
            set
            {
                this._Instruction.FooterPatterns = value;
            }
        }

        /// <summary></summary>
        public bool AcrossMultiPages
        {
            get
            {
                return this._Instruction.AcrossMultiPages;
            }
            set
            {
                this._Instruction.AcrossMultiPages = value;
            }
        }

        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._Instruction.ConnStr;
            }
            set
            {
                this._Instruction.ConnStr = value;
            }
        }

        /// <summary></summary>
        public string SelectSql
        {
            get
            {
                return this._Instruction.SelectSql;
            }
            set
            {
                this._Instruction.SelectSql = value;
            }
        }

        /// <summary></summary>
        public string IDField
        {
            get
            {
                return this._Instruction.IDField;
            }
            set
            {
                this._Instruction.IDField = value;
            }
        }

        /// <summary></summary>
        public string FilePathField
        {
            get
            {
                return this._Instruction.FilePathField;
            }
            set
            {
                this._Instruction.FilePathField = value;
            }
        }

        /// <summary></summary>
        public string TgtConnStr
        {
            get
            {
                return this._Instruction.TgtConnStr;
            }
            set
            {
                this._Instruction.TgtConnStr = value;
            }
        }

        /// <summary></summary>
        public string TgtTableName
        {
            get
            {
                return this._Instruction.TgtTableName;
            }
            set
            {
                this._Instruction.TgtTableName = value;
            }
        }

        /// <summary></summary>
        public string TgtIDField
        {
            get
            {
                return this._Instruction.TgtIDField;
            }
            set
            {
                this._Instruction.TgtIDField = value;
            }
        }

        /// <summary></summary>
        public string TgtContentField
        {
            get
            {
                return this._Instruction.TgtContentField;
            }
            set
            {
                this._Instruction.TgtContentField = value;
            }
        }

        /// <summary></summary>
        public string FlagField
        {
            get
            {
                return this._Instruction.FlagField;
            }
            set
            {
                this._Instruction.FlagField = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public PdfSectionFilterComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.TextMining.ToString();
            this.ComponentIcon = Resources.apds_206;
            this.ComponentImage = Resources.SqlWb_10008;
            this.ComponentName = "PDF Section Extractor";
            this.Description = "Extract pdf content as text based on header/footer patterns";

            this._Instruction =
                new PdfSectionFilterInstruction(this.ComponentID, Guid.Empty,
                                                string.Empty, string.Empty, string.Empty, string.Empty,
                                                string.Empty, string.Empty, string.Empty, string.Empty,
                                                string.Empty,
                                                new List<string>(), new List<string>(), true);

            this._Output =
                new PdfSectionFilterOutput(this.ComponentID, Guid.Empty, 0, 0);
        }
        #endregion

        #region Overrides of WorkflowComponentBase
        /// <summary><para>must implementable method to serialize activity settings to xml</para></summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectSql", this.SelectSql);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IDField", this.IDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePathField", this.FilePathField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtConnStr", this.TgtConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtTableName", this.TgtTableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtIDField", this.TgtIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtContentField", this.TgtContentField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FlagField", this.FlagField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AcrossPages", this.AcrossMultiPages.ToString());

            if (this.HeaderPatterns != null && this.HeaderPatterns.Count > 0)
            {
                XmlNode headerRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "HeaderPatterns");
                foreach (string headerPattern in this.HeaderPatterns)
                {
                    XmlNode headerNode = XmlDataUtil.AddElement(ref xDoc, headerRootNode, "HeaderPattern");
                    XmlDataUtil.UpdateAttribute(ref xDoc, headerNode, "Pattern", headerPattern);
                }
            }
            if (this.FooterPatterns != null && this.FooterPatterns.Count > 0)
            {
                XmlNode footerRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "FooterPatterns");
                foreach (string footerPattern in this.FooterPatterns)
                {
                    XmlNode footerNode = XmlDataUtil.AddElement(ref xDoc, footerRootNode, "FooterPattern");
                    XmlDataUtil.UpdateAttribute(ref xDoc, footerNode, "Pattern", footerPattern);
                }
            }


            return xNode;
        }

        /// <summary><para>subclass must implement this method to instantiate activity from xml</para></summary>
        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfSectionFilterComponent wfComponent = base.Instantiate(xNode) as PdfSectionFilterComponent;

                wfComponent.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                wfComponent.SelectSql = XmlDataUtil.GetAttributeValue(xNode, "SelectSql", "");
                wfComponent.FilePathField = XmlDataUtil.GetAttributeValue(xNode, "FilePathField", "");
                wfComponent.IDField = XmlDataUtil.GetAttributeValue(xNode, "IDField", "");
                wfComponent.TgtConnStr = XmlDataUtil.GetAttributeValue(xNode, "TgtConnStr", "");
                wfComponent.TgtTableName = XmlDataUtil.GetAttributeValue(xNode, "TgtTable", "");
                wfComponent.TgtIDField = XmlDataUtil.GetAttributeValue(xNode, "TgtIDField", "");
                wfComponent.TgtContentField = XmlDataUtil.GetAttributeValue(xNode, "TgtContentField", "");
                wfComponent.FlagField = XmlDataUtil.GetAttributeValue(xNode, "FlagField", "");

                XmlNodeList headerNodes = xNode.SelectNodes("HeaderPatterns/HeaderPattern");
                if (headerNodes != null && headerNodes.Count > 0)
                {
                    wfComponent.HeaderPatterns = new List<string>();
                    foreach (XmlNode headerNode in headerNodes)
                    {
                        wfComponent.HeaderPatterns.Add(XmlDataUtil.GetAttributeValue(headerNode, "Pattern", ""));
                    }
                }
                XmlNodeList footerNodes = xNode.SelectNodes("FooterPatterns/FooterPattern");
                if (footerNodes != null && footerNodes.Count > 0)
                {
                    wfComponent.FooterPatterns = new List<string>();
                    foreach (XmlNode footerNode in footerNodes)
                    {
                        wfComponent.FooterPatterns.Add(XmlDataUtil.GetAttributeValue(footerNode, "Pattern", ""));
                    }
                }

                return wfComponent;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}